package com.ccdt.tv.roundprogram.util;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.support.annotation.NonNull;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * 时间的工具类，方法需要重构，方法名称令人不解
 */

public class TimeUtil {


    public static Long getTimeryyyyMMddHHmmss(String time){
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        Long beginUseTime = null;
        try {
            beginUseTime = df.parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return beginUseTime;
    }


    public static String getNowTimeryyyyMMddHHmmss(){
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        String date = df.format(new Date());
        return date;
    }


    public static String getNowTimeryyyyMMddHHmm(){
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmm");//设置日期格式
        String date = df.format(new Date());
        return date;
    }

    public static String getNowTimeryyyyMMddHH(){
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHH");//设置日期格式
        String date = df.format(new Date());
        return date;
    }


    public static String getNowTimerddHHmmss(){
        SimpleDateFormat df = new SimpleDateFormat("ddHHmmss");//设置日期格式
        String date = df.format(new Date());
        return date;
    }

    public static String getNowTimeryyyyMMdd(){
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
        String date = df.format(new Date());
        return date;
    }

    public static String getNowTimeryyyyMMdd_TIME(int time){
        long currentTime = System.currentTimeMillis() ;
        currentTime +=time*60*60*1000;
        Date date=new Date(currentTime);
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        return df.format(date);
    }




    public static String getNowTimeryyyyMMdd_NEXT(int i){
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, i);
        return df.format(c.getTime());
    }


    public static String getFormatTimeHHMM(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.CHINA);
        return format.format(date);
    }

    public static String getFormatTimeHHMM(long date) {
        SimpleDateFormat format = new SimpleDateFormat("HHmm", Locale.CHINA);
        return format.format(date);
    }

    public static String getFormatTimeyyyyMMdd(long date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);
        return format.format(date);
    }

    public static String getFormatTimeyyyyMMddHHmm(long date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm", Locale.CHINA);
        return format.format(date);
    }

    public static String getFormatTimeMMDD(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("MM-dd", Locale.CHINA);
        return format.format(date);
    }

    public static String getFormatTimeMMDD(long date) {
        SimpleDateFormat format = new SimpleDateFormat("MM-dd", Locale.CHINA);
        return format.format(date);
    }

    public static Date[] getOneDayDuration(int todayOffset) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, todayOffset);
        Calendar startTime = (Calendar) calendar.clone();
        Calendar endTime = (Calendar) calendar.clone();
        startTime.set(Calendar.HOUR_OF_DAY, 0);
        startTime.set(Calendar.MINUTE, 0);
        startTime.set(Calendar.SECOND, 0);
        endTime.set(Calendar.HOUR_OF_DAY, 23);
        endTime.set(Calendar.MINUTE, 59);
        endTime.set(Calendar.SECOND, 59);
        return new Date[]{startTime.getTime(), endTime.getTime()};
    }

    public static String getDateStringByFormat(String format) {
        return getDateStringByFormat(format, 0);
    }

    public static String getDateStringByFormat(String format, int todayOffset) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, todayOffset);
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);
        return dateFormat.format(calendar.getTime());
    }

    public static String getWeekdayString(int todayOffset) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, todayOffset);
        int weekday = calendar.get(Calendar.DAY_OF_WEEK);
        String weekdayStr = "";
        switch (weekday) {
            case Calendar.SUNDAY:
                weekdayStr = "周日";
                break;
            case Calendar.MONDAY:
                weekdayStr = "周一";
                break;
            case Calendar.TUESDAY:
                weekdayStr = "周二";
                break;
            case Calendar.WEDNESDAY:
                weekdayStr = "周三";
                break;
            case Calendar.THURSDAY:
                weekdayStr = "周四";
                break;
            case Calendar.FRIDAY:
                weekdayStr = "周五";
                break;
            case Calendar.SATURDAY:
                weekdayStr = "周六";
                break;
        }
        return weekdayStr;
    }

    public static String getTimeString(Date date) {
        String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);
        return dateFormat.format(date);
    }

    public static String getTimeString(long date) {
        String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);
        return dateFormat.format(date);
    }


    public static Date getDataFromTimeStr(String timeStr) {
        //"yyyy/MM/dd HH:mm"
        int year = Integer.parseInt(timeStr.substring(0, 4));
        int month = Integer.parseInt(timeStr.substring(5, 7));
        int day = Integer.parseInt(timeStr.substring(8, 10));
        int hour = Integer.parseInt(timeStr.substring(11, 13));
        int min = Integer.parseInt(timeStr.substring(14, 16));
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, min);
        return calendar.getTime();
    }


    public static int StringData(){
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        int mWay = c.get(Calendar.DAY_OF_WEEK);
        return mWay-1;
    }


    public static int getDataFromeTimeStrText(String timeStr){
        int year = Integer.parseInt(timeStr.substring(0, 4));
        int month = Integer.parseInt(timeStr.substring(4, 6));
        int day = Integer.parseInt(timeStr.substring(6, 8));
        int hour = Integer.parseInt(timeStr.substring(8, 10));
        int min = Integer.parseInt(timeStr.substring(10, 12));
        int s = Integer.parseInt(timeStr.substring(12, 13));
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day, hour, min, s);
        return (int) calendar.getTimeInMillis();
    }

    public static Date getDataFromTimeStryyyyMMddHHmmss(String timeStr) {
        //"yyyyMMddHHmmss
        int year = Integer.parseInt(timeStr.substring(0, 4));
        int month = Integer.parseInt(timeStr.substring(4, 6));
        int day = Integer.parseInt(timeStr.substring(6, 8));
        int hour = Integer.parseInt(timeStr.substring(8, 10));
        int min = Integer.parseInt(timeStr.substring(10, 12));
        int s = Integer.parseInt(timeStr.substring(12, 13));
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, min);
        calendar.set(Calendar.SECOND, s);
        return calendar.getTime();
    }


    /**
     * 获取当前时间
     */
    @NonNull
    public static String getTimeNowString() {
        Calendar cal = Calendar.getInstance();
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        return ((hour < 10 ? ("0" + hour) : hour) + " : " +
                (minute < 10 ? ("0" + minute) : minute) );
    }


    // 开启轮询服务
    public static void startPollingService(Context context, int seconds, Class<?> cls, String action) {
        // 获取AlarmManager系统服务
        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        // 包装需要执行Service的Intent
        Intent intent = new Intent(context, cls);
        intent.setAction(action);
//      PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        // 触发服务的起始时间
        long triggerAtTime = SystemClock.elapsedRealtime();
        // 使用AlarmManger的setRepeating方法设置定期执行的时间间隔（seconds秒）和需要执行的Service
        manager.setRepeating(AlarmManager.ELAPSED_REALTIME , triggerAtTime, seconds * 1000, pendingIntent);
    }

    // 停止轮询服务
    public static void stopPollingService(Context context, Class<?> cls, String action) {
        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, cls);
        intent.setAction(action);
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        // 取消正在执行的服务
        manager.cancel(pendingIntent);
    }


}
